//
//  AppealView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/3.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppealView: UIView, ReasonPhotoDelegate {
    var dataSource: AppealViewController!
    
    lazy var backView: UIView = {
      let view = UIView()
      view.themeBackground = ThemeManager.ThemeColorKeys.background
      return view
    }()
    
    lazy var contentLabel: UILabel = {
        let label = BaseLabel(style:.twiceStyle)
       label.font = UIFont.systemFont(ofSize: 12)
       label.text = KAppealText
       label.numberOfLines = 0
       label.changeContentSpace(space: 6)
       return label
    }()
    
    lazy var reasonPhotoView :ReasonPhotoView = {
       let photoView = ReasonPhotoView(dataSource: self)
        photoView.delegate = self
        return photoView
    }()
    
    lazy var reasonBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(selectAppealRes), for: .touchUpInside)
        return btn
    }()
    lazy var reasonLabel: UILabel = {
        let label = BaseLabel(style: .oneStyle)
        label.text = kText("申诉原因")
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    }()
    
    lazy var arrowImage: UIImageView = {
        let image = UIImageView(image: #imageLiteral(resourceName: "arrow_right"))
        return image
    }()
    
    lazy var selectBtn:UIButton = {
        let btn =  CommonBtn.creatSelectBtnWithTitle(title: kText("发送"), y: 435, view: self, sel:  #selector(selectBtnAction))
        return btn
    }()
    
    lazy var selectLabel: UILabel = {
        let label = UILabel()
        label.text = kText("请选择申诉原因")
        label.numberOfLines = 0
        label.textAlignment = .right
//        label.adjustsFontSizeToFitWidth = true
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = kTextColor
        return label
    }()
    init(dataSource: AppealViewController) {
        super.init(frame: CGRect.zero)
        self.dataSource = dataSource
        self.setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI() -> Void {

        self.themeBackground = ThemeManager.ThemeColorKeys.background
        self.addSubview(backView)
        
        backView.themeBackground = ThemeManager.ThemeColorKeys.background3
        backView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(88)
        }
        
        backView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        self.addSubview(reasonBtn)
        reasonBtn.snp.makeConstraints { (make) in
            make.top.equalTo(backView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
        }
        
        reasonBtn.addSubview(reasonLabel)
        reasonLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
        }
        
        reasonBtn.addSubview(arrowImage)
        arrowImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        reasonBtn.addSubview(selectLabel)
        selectLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(reasonLabel.snp.right).offset(10)
            make.right.equalTo(arrowImage).offset(-16)
        }
        
        self.addSubview(selectBtn)
        
        self.addSubview(reasonPhotoView)
        reasonPhotoView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(149)
            make.left.right.equalToSuperview()
            make.height.equalTo(260)
        }
    }
    //ReasonPhotoDelegate
    func selectPhoto() {
        SelectPhotoManager.manager.selectPhoto(dataSource: dataSource, maximum: 3, haveNb: reasonPhotoView.photoArr.count) { (arr) in
            if arr.count == 0 {
                return
            }
            self.reasonPhotoView.photoArr =  self.reasonPhotoView.photoArr + arr
            self.reasonPhotoView.changePhoto()
        }
    }
    

    ///选择申诉原因
    @objc func selectAppealRes() {
        SelectActionManger.manager.creatSheet(style: .commonSty, titleArr: KAppealResArr, selectStr: selectLabel.text, dataSource: self)
    }
    
    @objc func selectBtnAction() {
        let res = self.selectLabel.text ?? ""
        if !KAppealResArr.contains(res) {
            CYMBShow.showMessage(kText("请输入申诉原因"))
            return
        }
        let des = self.reasonPhotoView.textView.text ?? ""
        if des.count <= 0  {
            CYMBShow.showMessage(kText("请输入申诉描述详情"))
            return
        }
        let images = self.reasonPhotoView.photoArr
//        if images.count < 3 {
//            CYMBShow.showMessage(kText("请选择3张申诉截图"))
//            return
//        }
        self.dataSource.viewModel?.uploadAppelImage(res: res, des: des, images: images)
    }

}

extension AppealView : SelectmangerDelegate {
    func selectModel(model:SheetModel) {
        self.selectLabel.text = model.selectStr
    }
}
